package net.chrone.quickpayapi.service.impl;

import java.util.Date;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import net.chrone.quickpayapi.model.Order;
import net.chrone.quickpayapi.model.PayChannel;
import net.chrone.quickpayapi.service.PayChannelService;
import net.chrone.quickpayapi.service.RiskService;
import net.chrone.quickpayapi.util.DateUtils;

@Service
public class RiskServiceImpl implements RiskService {
	
	private final Logger logger = Logger.getLogger(getClass());

	@Autowired
	private PayChannelService payChannelService;
	
	/**
	 * 风控检查
	 */
	@Override
	public String riskCheck(Order order) {
		PayChannel payChannel = payChannelService.find(order);
		if(order.getAmount()<payChannel.getMinAmount()){
			logger.debug("交易金额小于渠道最低金额,无法交易");
			return "701";
		}
		if(order.getAmount()>payChannel.getMaxAmount()){
			logger.debug("交易金额大于渠道最高金额,无法交易");
			return "702";
		}
		Date theDate = DateUtils.parseDate(DateUtils.DateToString(new Date(),"HH:mm:ss"), "HH:mm:ss");
		Date startDate = DateUtils.parseDate(DateUtils.DateToString(payChannel.getStartTime(),"HH:mm:ss"), "HH:mm:ss");
		Date endDate = DateUtils.parseDate(DateUtils.DateToString(payChannel.getEndTime(),"HH:mm:ss"), "HH:mm:ss");
		
		if(theDate.compareTo(startDate)==-1){
			logger.debug("当前时间渠道不允许交易");
			return "703";
		}
		
		if(theDate.compareTo(endDate)==1){
			logger.debug("当前时间渠道不允许交易");
			return "703";
		}
		
		return null;
	}
	
}
